﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class admin_AdvertiseAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AdvertiseBLL bll = new AdvertiseBLL();
            Advertise advertise = bll.Get();
            if (advertise != null)
            {
                lblPic.Text = advertise.Content;
                txtLink.Text = advertise.Link;
            }
            else
            {
                lblPic.Text = "";
                txtLink.Text = "";
            }
            txtId.Text = "Advertise";
            txtId.ReadOnly = true;
            lblPic.ReadOnly = true;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Advertise advertise = new Advertise();
        advertise.AdvertiseId = txtId.Text;
        advertise.Link = txtLink.Text;
        string path = Server.MapPath("~/videouploadImg/");
        Boolean fileOk = false;
        //判断是否已经选取文件
        if (flImgSwf.HasFile == true)
        {
            //取得文件的扩展名,并转换成小写
            string fileExtension = System.IO.Path.GetExtension(this.flImgSwf.FileName).ToLower();
            //限定只能上传图片类型
            Dictionary<string, string> myDic = new Dictionary<string, string>();
            myDic.Add("1", ".jpg");
            myDic.Add("2", ".gif");
            myDic.Add("3", ".jpeg");
            myDic.Add("4", ".bmp");
            myDic.Add("5", ".png");
            myDic.Add("6", ".swf");
            if (myDic.ContainsValue(fileExtension))
            {
                fileOk = true;
            }
            if (fileOk)
            {
                try
                {
                    string fileName = "advertise" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + fileExtension;
                    string temp = path + fileName;
                    this.flImgSwf.PostedFile.SaveAs(temp);
                    StringBuilder sb = new StringBuilder();
                    if (fileExtension == ".swf")
                    {
                        sb.AppendLine("<object width=\"710\" height=\"180\" style=\"float: left\"><param name=\"movie\" value=\"uploadimages/" + fileName + "\"></param>");
                        sb.AppendLine("<param name=\"allowScriptAccess\" value=\"sameDomain\"></param>");
                        sb.AppendLine("<param name=\"wmode\" value=\"transparent\"></param>");
                        sb.AppendLine("<embed src=\"uploadimages/" + fileName + "\" type=\"application/x-shockwave-flash\" width=\"710\" height=\"180\" allowfullscreen=\"true\" wmode=\"transparent\" allowscriptaccess=\"always\"></embed></object>");
                    }
                    else
                        sb.AppendLine("<a href=\"" + txtLink.Text + "\" target=\"_blank\"><img src=\"uploadimages/" + fileName + "\" width=\"710\" height=\"180\" border=\"0\"/></a>");
                    advertise.Content = sb.ToString();
                    AdvertiseBLL bll = new AdvertiseBLL();
                    if (bll.Update(advertise) > 0)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加成功');location.href=location.href;", true);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(GetType(), "save", "alert('广告ID已存在');", true);
                    }
                }
                catch (Exception)
                {
                    ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加失败,请重试');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('请选择正确的文件格式!');", true);
            }
        }
        else
        {
            if (lblPic.Text == "")
            {
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('未选定上传图片或flash文件');'", true);
            }
            else
            {
                advertise.Content = lblPic.Text;
                AdvertiseBLL bll = new AdvertiseBLL();
                if (bll.Update(advertise) > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加成功');location.href=location.href;", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加失败！');", true);
                }
            }
        }
    }
}
